﻿using System.ServiceModel;
using Nop.Plugin.Payments.PayPalHosted.PayPalAPI;

namespace Nop.Plugin.Payments.PayPalHosted.Services
{
    public class PayPalInterfaceService : IPayPalInterfaceService
    {
        private readonly PayPalHostedPaymentSettings _payPalHostedPaymentSettings;

        public PayPalInterfaceService(PayPalHostedPaymentSettings payPalHostedPaymentSettings)
        {
            _payPalHostedPaymentSettings = payPalHostedPaymentSettings;
        }

        public PayPalAPIAAInterfaceClient GetAAService()
        {
            return
                new PayPalAPIAAInterfaceClient(new BasicHttpsBinding(),
                                               new EndpointAddress(_payPalHostedPaymentSettings.IsLive
                                                                       ? "https://api-3t.paypal.com/2.0/"
                                                                       : "https://api-3t.sandbox.paypal.com/2.0/"));
        }

        public PayPalAPIInterfaceClient GetService()
        {
            return
                new PayPalAPIInterfaceClient(new BasicHttpsBinding(),
                                               new EndpointAddress(_payPalHostedPaymentSettings.IsLive
                                                                       ? "https://api-3t.paypal.com/2.0/"
                                                                       : "https://api-3t.sandbox.paypal.com/2.0/"));
        }


        public CustomSecurityHeaderType GetRequesterCredentials()
        {
            return new CustomSecurityHeaderType
                       {
                           Credentials = _payPalHostedPaymentSettings.DoNotHaveBusinessAccount
                                             ? new UserIdPasswordType
                                                   {
                                                       Subject = _payPalHostedPaymentSettings.EmailAddress,
                                                   }
                                             : new UserIdPasswordType
                                                   {
                                                       Signature = _payPalHostedPaymentSettings.ApiSignature,
                                                       Username = _payPalHostedPaymentSettings.Username,
                                                       Password = _payPalHostedPaymentSettings.Password,
                                                   }
                       };
        }
    }
}